package com.somnus.androidkotlindemo.databinding

import android.databinding.DataBindingUtil
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import com.somnus.androidkotlindemo.R
import com.somnus.androidkotlindemo.bean.StaffBean
import com.somnus.androidkotlindemo.bean.User
import kotlinx.android.synthetic.main.activity_databinding_layout.*


/***
 * 使用DataBinding
 */
class DatabindingActivity : AppCompatActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val databinding = DataBindingUtil.setContentView<ActivityDatabindingLayoutBinding>(
                this@DatabindingActivity, R.layout.activity_databinding_layout)

        val mStaff = StaffBean()
//        mUser.userName = "张三"
        databinding.staff = mStaff


        button6.setOnClickListener {
            mStaff.staffAge = 20
            mStaff.setStaffName("我是更改后的名字，但是age更改不了")
        }
        button7.setOnClickListener {
            mStaff.staffAge = 50
            mStaff.staffName = "我是通过notifyChange更改的Name"
            mStaff.staffNickName = "notifyChange修改的NickName"
        }
        val mUser = User()
        databinding.user = mUser
        databinding.myHandler = MyHandler

        button8.setOnClickListener({
            mUser.userName.set("张三")
        })
//        button9.setOnClickListener({
//
//        })


    }

    companion object MyHandler {
        fun changedName(_user: User) {
//            _user.userName.set("张三修改")
            _user.userNickName.set("张三NickName")
            _user.userName.set("changedName")
        }
    }
}
